<?php

require_once(WSB_XTENSION_PATH .'login/forms/ResetPassword.php');

class Login_ChangePasswordController extends WSB_Xtension_Static
{

	public function init()
	{
		$this->initTranslate();
	}

	public function indexAction(Login_Form_ResetPassword $form = null)
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		if(is_null($form)) {
			$form = new Login_Form_ResetPassword();
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$form->setDefaults($feuser->toArray());
		}
		$this->assignToView('form', $form);
		$this->renderView('change_password/index.phtml');
	}

	public function saveAction()
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		$request = $this->getRequest();
		$form = new Login_Form_ResetPassword();
		if($request->isPost() && $form->isValid($request->getPost())) {
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$feuser->password = $form->getValue('password');
			$feuser->setTable(new Member_Model_DbTable_Feuser());
			$feuser->save();
			$this->renderView('change_password/changed.phtml');
		} else {
			$this->indexAction($form);
		}
	}

}
